library(ggplot2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble 3.1.6 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.4 ✓ stringr 1.4.0
## ✓ readr 2.1.1 ✓ forcats 0.5.1
## ✓ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(broom)
## Warning: package 'broom' was built under R version 4.1.2
movie_budgets <-read.csv("https://jfukuyama.github.io/teaching/stat670/assignments/movie_budgets.txt", sep = " " )
head(movie_budgets)
## title year length budget
## 1 'G' Men 1935 85 450000
## 2 'Manos' the Hands of Fate 1966 74 19000
## 3 'Til There Was You 1997 113 23000000
## 4 .com for Murder 2002 96 5000000
## 5 10 Things I Hate About You 1999 97 16000000
## 6 100 Mile Rule 2002 98 1100000
ggplot(movie_budgets, aes(x = year, y = log10(budget))) + geom_point() + stat_smooth(method="loess", span=0.35, se=FALSE, aes(color="blue")) + stat_smooth(method="lm",span = 0.35 ,method.args = list(degree = 2), se=FALSE, aes(color="green")) + scale_color_identity(name="Model Fitting", breaks=c("blue","green"),labels=c("LOESS","Linear"), guide="legend")
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## Warning: In lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok,
## ...) :
## extra argument 'degree' will be disregarded

ggplot(movie_budgets, aes(x = length, y = log10(budget))) + geom_point() + stat_smooth(method="loess", span=0.35, se=FALSE, aes(color="blue")) + stat_smooth(method="lm",span = 0.35 ,method.args = list(degree = 2), se=FALSE, aes(color="green")) + scale_color_identity(name="Fitting", breaks=c("blue","green"),labels=c("LOESS","Linear"), guide="legend")
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## Warning: In lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok,
## ...) :
## extra argument 'degree' will be disregarded

ggplot(movie_budgets, aes(y = log10(budget), x = year)) + geom_point(alpha = 0.5) +
facet_wrap(~ cut_number(length, n = 10), nrow = 3) +stat_smooth(method = "loess",span=0.3, se = FALSE, method.args = list(degree = 2),aes(color = "blue")) + stat_smooth(method = "lm", se = FALSE, aes(color = "green")) + scale_color_identity(name="Fitting", breaks=c("blue","green"),labels=c("LOESS","Linear"), guide="legend")
## `geom_smooth()` using formula 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 2005.5
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 1.465
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : There are other near singularities as well. 1
## `geom_smooth()` using formula 'y ~ x'

ggplot(movie_budgets, aes(y = log10(budget), x = length)) +geom_point(alpha = 0.5) + facet_wrap(~ cut_number(year, n = 10), nrow = 3) + stat_smooth(method = 'loess',span=0.3, se = FALSE, method.args = list(degree = 2, family = "symmetric"), aes(color = 'blue')) + stat_smooth(method = 'lm', se = FALSE, aes(color = 'green')) + scale_color_identity(name = "Fitting",breaks = c("blue", "green"),labels = c("LOESS", "Linear"), guide = "legend") + ggtitle("log10Budget over length condition on year")
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'

movie_budgets$log_budget = log10(movie_budgets$budget)
movie_budgets_lo = loess((log_budget) ~ length*year, data = movie_budgets, span = 0.35, family = 'symmetric', degree = 2)
prediction_grid = data.frame(expand.grid(year = seq(1906, 2005, 10), length = seq(1, 390, 1), budget =seq(2,10,1)))
pred_movie_budgets <- augment(movie_budgets_lo, newdata = prediction_grid)
ggplot(pred_movie_budgets) + geom_line(aes(x = length, y = .fitted)) + facet_wrap(~year) + labs(x = "Length", y = "Fitted Values")

ggplot(pred_movie_budgets, aes(x = length, y = year, fill = .fitted)) + geom_raster() + facet_wrap(~ cut_number(.fitted, n = 9, dig.lab = 4)) + xlab("Length") + ylab("Year") + scale_fill_viridis_b("Fitted Budget") + ggtitle("Fitted Values of Movie Dat's Raster Plot")

ggplot(pred_movie_budgets, aes(x = length, y = year, fill = .fitted, z = .fitted)) + geom_raster() + geom_contour(bins = 30, color = "black") + scale_fill_viridis_c("Log10 Budget of Movie Data") + xlab("Length") + ylab("Year") + ggtitle("Fitted values of Movie Data's Contour plot")
